package com.mrdeer.iot.service;

import com.mrdeer.common.tools.JsonUtils;
import com.mrdeer.common.tools.RedisService;
import com.mrdeer.iot.bean.command.Protocol;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author: xie jianchu
 */
@Slf4j
@Service
public class IotService {

    @Resource
    private RedisService redisService;

    /**
     * 加载 IoT 映射信息到 全局
     *
     * @param host
     * @param port
     * @return
     */
    public boolean loadIotMapper2Global(String host, Integer port) throws Exception {

        return true;
    }

    /**
     * 刷新 全局 IoT 映射信息
     *
     * @return
     * @throws Exception
     */
    public boolean refreshIotMapper2Global() throws Exception {

        return true;
    }

    /**
     * 获取 IoT 映射信息
     */
    private Map<String, Protocol> getIoTSnIdMapper(String key) throws Exception {
        log.info("get IoT SnId Mapper... key is [{}]", key);
        Map<Object, Object> mapperMap = redisService.hGetAll(key);
        if (CollectionUtils.isEmpty(mapperMap)) {
            log.warn("the IoTSnIdMapper is empty!");
            return new HashMap<>(0);
        }

        Map<String, Protocol> mapperStrMap = new HashMap<>(mapperMap.size());
        Set<Object> keySet = mapperMap.keySet();
        for (Object sn : keySet) {
            Object data = mapperMap.get(sn);
            Protocol dataMap = JsonUtils.jsonStr2Obj(String.valueOf(data), Protocol.class);
            mapperStrMap.put(String.valueOf(sn), dataMap);
        }
        return mapperStrMap;
    }
}
